package com.kinptm.masterclock.adapter;

import android.view.LayoutInflater;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.List;

public class HolderViewFiller<T> {

    /**
     * 从XML中创建对象 *
     */
    private LayoutInflater mInflater;

    /**
     * View创建器 *
     */
    private ViewCreator<T> mCreator;

    public HolderViewFiller(LayoutInflater inflater, ViewCreator<T> creator) {
        mInflater = inflater;
        mCreator = creator;
    }

    /**
     * </br><b>description :</b>将数据更新到View中
     * </br><b>time :</b> 2012-7-18 下午7:41:55
     *
     * @param view
     * @param data
     */
    public void update(ListView view, List<T> data) {
        HolderAdapter<T> holderAdapter = exportAdapter(view);
        if (null != holderAdapter) {
            holderAdapter.update(data);
        }
    }

    /**
     * </br><b>description :</b>添加数据集
     * </br><b>time :</b> 2012-7-18 下午8:16:38
     *
     * @param view
     * @param set
     */
    public void add(ListView view, List<T> set) {
        HolderAdapter<T> holderAdapter = exportAdapter(view);
        if (null != holderAdapter) {
            holderAdapter.add(set);
        }
    }

    /**
     * </br><b>title : </b> 添加数据
     * </br><b>description :</b>添加数据
     * </br><b>time :</b> 2012-7-18 下午8:16:55
     *
     * @param view
     * @param item
     */
    public void add(ListView view, T item) {
        HolderAdapter<T> holderAdapter = exportAdapter(view);
        if (null != holderAdapter) {
            holderAdapter.add(item);
        }
    }

    @SuppressWarnings("unchecked")
    public HolderAdapter<T> exportAdapter(ListView view) {
        ListAdapter adapter = view.getAdapter();
        HolderAdapter<T> holderAdapter = null;
        try {
            holderAdapter = (null == adapter) ? null : (HolderAdapter<T>) adapter;
            if (null == holderAdapter) {
                holderAdapter = new HolderAdapter<T>(mInflater, mCreator);
                view.setAdapter(holderAdapter);
            }
        } catch (ClassCastException ex) {
            throw new IllegalArgumentException(
                    String.format("Adapter in View(%s) is not a HolderAdapter!", view.getClass().getName()));
        }
        return holderAdapter;
    }
}
